from __future__ import division
import numpy as np
import math
elements = ['H', 'N', 'C', 'O','S','H2O','N2','O2','Ar','CO2','O','OH','H','H2','N','NO','NO2','CH4','SO2','SO3','CO']
MolarMass = [1.00794, 14.0067, 12.0107, 15.9994, 32.065,18.02,28.01348,31.9988,39.948,44.0098,15.9994,17.00734,1.00794,2.01588,14.00674,30.00614,46.00554,16.04276,64.06480,80.06420,28.01040]
molarMass = {elements[x]:MolarMass[x] for x in range(len(elements))}

def IG_props(prop,fluid,T,molar):
    if (fluid=='N2'):
        if (T>1000):
            #coef=np.array([0.02926640E+02,0.14879768E-02,-0.05684760E-05,0.10097038E-09,-0.06753351E-13,-0.09227977E+04 ,0.05980528E+02] , dtype=float)
            coef=np.array([2.95257626E+00,1.39690057E-03,-4.92631691E-07,7.86010367E-11,-4.60755321E-15,-9.23948645E+02 ,5.87189252E+00] , dtype=float)
        else:
            coef = np.array([0.03298677E+02,0.14082404E-02,-0.03963222E-04,0.05641515E-07,-0.02444854E-10,-0.10208999E+04, 0.03950372E+02] , dtype=float)
    elif(fluid=='O2'):
        if (T>1000):
            coef=np.array([3.66096083E+00,6.56365523E-04,-1.41149485E-07,2.05797658E-11,-1.29913248E-15 ,-1.21597725E+03,3.41536184E+00] , dtype=float)            
#            coef=np.array([3.28253784E+00,1.48308754E-03,-7.57966669E-07,2.09470555E-10,-2.16717794E-14 ,-1.08845772E+03,5.45323129E+00] , dtype=float)
        else:
            coef = np.array([3.78245636E+00,-2.99673416E-03,9.84730201E-06,-9.68129509E-09,3.24372837E-12,-1.06394356E+03,3.65767573E+00] , dtype=float)
    elif(fluid=='CO2'):
        if (T>1000):
            coef=np.array([4.63659493E+00,2.74131991E-03,-9.95828531E-07,1.60373011E-10,-9.16103468E-15,-4.90249341E+4,-1.93534855E+00] , dtype=float)      
#            coef=np.array([3.85746029E+00,4.41437026E-03,-2.21481404E-06,5.23490188E-10,-4.72084164E-14,-4.87591660E+04,2.27163806E+00] , dtype=float)
        else:
            coef = np.array([2.35677352E+00,8.98459677E-03,-7.12356269E-06, 2.45919022E-09,-1.43699548E-13,-4.83719697E+04,9.90105222E+00    ] , dtype=float)
    elif(fluid=='H2O'):
        if (T>1000):
#            coef=np.array([ 3.03399249E+00,2.17691804E-03,-1.64072518E-07,-9.70419870E-11 ,1.68200992E-14,-3.00042971E+04,4.96677010E+00] , dtype=float)
            coef=np.array([ 2.67703787E+00,2.97318329E-03,-7.73769690E-07,9.44336680E-11 ,-4.26900959E-15,-2.98858938E+04,6.882555710E+00] , dtype=float)
        else:
            coef = np.array([4.19864056E+00,-2.03643410E-03,6.52040211E-06,-5.48797062E-09,1.77197817E-12,-3.02937267E+04,-8.49032208E-01] , dtype=float)
    elif(fluid=='Ar'):
        if (T>1000):
            coef=np.array([ 0.02500000E+02, 0.00000000E+00, 0.00000000E+00 ,0.00000000E+00, 0.00000000E+00, -0.07453750E+04, 0.04366000E+02] , dtype=float)
        else:
            coef = np.array([ 0.02500000E+02, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00,-0.07453750E+04, 0.04366000E+02 ] , dtype=float)
    elif(fluid=='CO'):
        if (T>1000): 
            coef=np.array([3.04848583E+00, 1.35172818E-03, -4.85794075E-07, 7.88536486E-11,-4.69807489E-15,-1.42661171E+04,6.01709790E+00] , dtype=float)
        else:
            coef = np.array([3.57953347E+00,-6.10353680E-04,1.01681433E-06,9.07005884E-10,-9.04424499E-13,-1.43440860E+04, 3.50840928E+00] , dtype=float)
    elif(fluid=='O'):
        if (T>1000):  
            coef=np.array([2.54363697E+00,-2.73162486E-05,-4.19029520E-09,4.95481845E-12,-4.79553694E-16,2.92260120E+04,4.92229457E+00] , dtype=float)
        else:
            coef = np.array([3.16826710E+00, -3.27931884E-03, 6.64306396E-06,-6.12806624E-09, 2.11265971E-12,2.91222592E+04, 2.05193346E+00] , dtype=float)
    elif(fluid=='OH'):
        if (T>1000):  
            coef=np.array([2.83864607E+00, 1.10725886E-03,-2.93914978E-07,4.20524247E-11,-2.42169092E-15,3.94395852E+03,5.84452662E+00] , dtype=float)
        else:
            coef = np.array([3.99201543E+00,-2.40131752E-03,4.61793841E-06,-3.88113333E-09,1.36411470E-12,3.61508056E+03,-1.03925458E-01] , dtype=float)
    elif(fluid=='H'):
        if (T>1000):  
            coef = np.array([2.50000286E+00,-5.65334214E-09, 3.63251723E-12,-9.19949720E-16, 7.95260746E-20, 2.54736589E+04,-4.46698494E-01] , dtype=float)
        else:
            coef = np.array([2.50000000E+00, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00, 2.54736599E+04,-4.46682853E-01] , dtype=float)    
    elif(fluid=='H2'):
        if (T>1000):  
            coef = np.array([2.93286579E+00, 8.26607967E-04,-1.46402335E-07, 1.54100359E-11,-6.88804432E-16,-8.13065597E+02,-1.02432887E+00 ] , dtype=float)
        else:
            coef = np.array([2.34433112E+00, 7.98052075E-03,-1.94781510E-05, 2.01572094E-08,-7.37611761E-12,-9.17935173E+02, 6.83010238E-01] , dtype=float)      
    elif(fluid=='N'):
        if (T>1000):  
            coef = np.array([2.41594293E+00, 1.74890600E-04,-1.19023667E-07, 3.02262387E-11,-2.03609790E-15, 5.61337748E+04, 4.64960986E+00 ] , dtype=float)
        else:
            coef = np.array([2.50000000E+00, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00, 0.00000000E+00, 5.61046378E+04, 4.19390932E+00 ] , dtype=float)     
    elif(fluid=='NO'):
        if (T>1000):  
            coef = np.array([3.26071234E+00, 1.19101135E-03,-4.29122646E-07, 6.94481463E-11,-4.03295681E-15, 9.92143132E+03, 6.36900518E+00] , dtype=float)
        else:
            coef = np.array([4.21859896E+00,-4.63988124E-03, 1.10443049E-05,-9.34055507E-09, 2.80554874E-12, 9.84509964E+03, 2.28061001E+00] , dtype=float)    
    elif(fluid=='NO2'):
        if (T>1000):  
            coef = np.array([4.88474429E+00, 2.17241639E-03,-8.28079020E-07, 1.57477293E-10,-1.05110549E-14, 2.31648462E+03,-1.17357075E-01] , dtype=float)
        else:
            coef = np.array([3.94403907E+00,-1.58547444E-03, 1.66578984E-05,-2.04754478E-08, 7.83503265E-12, 2.89659865E+03, 6.31196225E+00] , dtype=float)    
    elif(fluid=='CH4'):
        if (T>1000):  
            coef = np.array([1.63552643E+00, 1.00842795E-02,-3.36916254E-06, 5.34958667E-10,-3.15518833E-14,-1.00056455E+04, 9.99313326E+00] , dtype=float)
        else:
            coef = np.array([5.14987613E+00,-1.36709788E-02, 4.91800599E-05,-4.84743026E-08, 1.66693956E-11,-1.02466476E+04,-4.64130376E+00] , dtype=float)    
    elif(fluid=='SO2'):
        if (T>1000):  
            coef = np.array([5.24513640E+00, 1.97042040E-03,-8.03757690E-07, 1.51499690E-10,-1.05580040E-14,-3.75582270E+04,-1.07404892E+00] , dtype=float)
        else:
            coef = np.array([3.26653380E+00, 5.32379020E-03, 6.84375520E-07,-5.28100470E-09, 2.55904540E-12,-3.69081480E+04, 9.66465108E+00] , dtype=float)    
    elif(fluid=='SO3'):
        if (T>1000):  
            coef = np.array([7.07573760E+00, 3.17633870E-03,-1.35357600E-06, 2.56309120E-10,-1.79360440E-14,-5.02113760E+04,-1.11875176E+01] , dtype=float)
        else:
            coef = np.array([2.57803850E+00, 1.45663350E-02,-9.17641730E-06,-7.92030220E-10, 1.97094730E-12,-4.89317530E+04, 1.22651384E+01] , dtype=float)    
    else:
        print("The fluid "+fluid+" does not exist")
    a1=coef[0]
    a2=coef[1]
    a3=coef[2]
    a4=coef[3]
    a5=coef[4]
    a6=coef[5]
    a7=coef[6]
    MM=molarMass[fluid]
    R=8314.4598/MM
    Cp = (a1 + a2*T + a3*T**2 + a4*T**3 + a5*T**4)*R
    H = (a1 + a2*T/2 + a3*T**2 /3+ a4*T**3 /4 + a5*T**4 /5 + a6/T)*R*T
    S = (a1*math.log(T) + a2*T + a3*T**2 /2 + a4*T**3 /3 + a5*T**4 /4 + a7)*R
    G = H-T*S
    if (prop=="H"):
        value=H
    elif(prop=="S"):
        value=S
    elif(prop=="Cp"):
        value=Cp
    elif(prop=="G"):
        value=G
    else:
        print('Wrong properties selected')
        value=0
    if(molar==1):
        value=value*MM
    return value